let P =require("bluebird");
let cnst = require('../const');
let phttp = require("./phttp")
let Token = require('../token');
let wxUtil = module.exports = {};

//获取用户access_token，两种方式
wxUtil.getAccessToken= P.coroutine(function*(code,os) {
    let getAsync= P.promisify(get_access_token);
    let res=yield getAsync(code,os);
    res.tick=new Date().getTime();
    let s=JSON.stringify(res);
    console.logw('access_info ',s);
    //yield app.redis.setHashItem(iconst.wxCodeCacheMap,code,s);
    return res;
});

wxUtil.getUserInfoByAccessToken= P.coroutine(function*(access_token,openid,ip,stoken,unionid) {
    let getAsync= P.promisify(get_state_info);
    let result;
    try {
        result= yield getAsync(access_token, openid);
        console.log("result ",result);
    } catch(e) {
        console.log("result ",e.message)
        return null;
    }
    return result;

});

function getDateTime() {
    let myDate=new Date();
    let sd=myDate.getFullYear()+"-"+myDate.getMonth()+"-"+myDate.getDay()+"-"+myDate.getHours()+"-"+myDate.getMinutes()+"-"+myDate.getSeconds();
    return sd;
}

function get_state_info(access_token,openid,callback){
    let data = {
        access_token:access_token,
        openid:openid
    };
    phttp.get2("https://api.weixin.qq.com/sns/userinfo",data,callback,true);
}

wxUtil.getAccessTokenFull= P.coroutine(function*(appid,secret,code,os,callback) {

    let getAsync= P.promisify(get_access_token_full);
    let result;
    try {
        result= yield getAsync(appid,secret,code,os);
        console.log("getAccessTokenFull result ",result);
    } catch(e) {
        console.log("getAccessTokenFull result ",e.message)
        return null;
    }
    return result;

})
function get_access_token_full(appid,secret,code,os,callback){

    console.logw('get_access_token');
    var data = {
        appid:appid,
        secret:secret,
        code:code,
        grant_type:"authorization_code"
    };
    if(os=='normal')
    {
        console.logw('get api accessToken');
        var data = {
            appid:appid,
            secret:secret,
            grant_type:"client_credential"
        };
        phttp.get2("https://api.weixin.qq.com/cgi-bin/token",data,function(suc,json)
        {
            console.logw('api access token ',json);
            if(json.errcode>0)
                callback(null,json);
            else
            {   console.logw('get_access_token ',json);
                callback(null,json);
            }
        },true);
        return;
    }
//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    phttp.get2("https://api.weixin.qq.com/sns/oauth2/access_token",data,function(suc,json)
    {
        if(json.errcode>0)
            callback(null,json);
        else
        {
            console.logw('get_access_token ',json);
            callback(null,json);
        }
    },true);
}

function get_access_token(code,os,callback){
    console.logw('get_access_token');
    let data = {
        appid: cnst.wx_appid,
        secret: cnst.wx_AppSecret,
        code:code,
        grant_type:"authorization_code"
    };
    //获取普通的access_token，需要用户关注公众号才能获取用户信息
    if(os === 'normal') {
        console.logw('get api accessToken');
        let data = {
            appid:cnst.wx_appid,
            secret:cnst.wx_AppSecret,
            grant_type:"client_credential"
        };
        phttp.get2("https://api.weixin.qq.com/cgi-bin/token",data,function(suc,json) {
            console.logw('api access token ',json);
            if(json.errcode>0)
                callback(-1,json);
            else {
                console.logw('get_access_token ',json);
                callback(null,json);
            }
        },true);
        return;
    }
    // 获取网页授权access_token,可以获取用户信息，需要先通过用户授权获取code值
    //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    phttp.get2("https://api.weixin.qq.com/sns/oauth2/access_token",data,function(suc,json) {
        if(json.errcode>0)
            callback(-1,json);
        else {
            console.logw('get_access_token ',json);
            callback(null,json);
        }
    },true);
}